<?php

namespace App\Service\Impl;

if (!defined('IN_PX'))
    exit;

use App\Service\Mobile;
use Phoenix\Log\Log4p as logger;

class MobileImpl implements Mobile {

    const VERSION = '1.1.2';

    private function __Service() {}

    private function __Inject($cache) {}

//    private $_url = 'http://sh2.ipyy.com/smsJson.aspx';
//    private $_account = 'jksc797';
//    private $_password = '851965';

    private $_url = 'https://sms.yunpian.com/v2/sms/single_send.json';
    private $_apikey = '3c30855714c7944e0570df585b539a46';

    /**
     *发送注册验证码
     */
    public function sendContent($mobile, $code, $codeType = 1, $other = array()) {
        $content = "【艺栢图库】您的验证码是：" . $code;
        switch ($codeType) {
            case 1:
                $content = "【艺栢图库】您的验证码是{$code}。如非本人操作，请忽略本短信";
                break;
        }
        $result = $this->send($mobile, $content);
        return $result;
    }

    private function send($mobile, $content){
        $body=array(
            'text'=>$content,
            'apikey'=>$this->_apikey,
            'mobile'=>$mobile
        );

        $ch = curl_init();
        /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));
        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $json_data = $this->lastSend($ch,$body);
        $array = json_decode($json_data,true);
        curl_close($ch);

        return $array;
    }

    private function lastSend($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }


}
